<script setup>
import {
  CaretBottom,
  Crop,
  EditPen,
  Management,
  Promotion,
  SwitchButton,
  User,
  UserFilled,
} from "@element-plus/icons-vue";
import {onMounted} from "vue";
import {useRouter} from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";
import {useUserStore} from "@/stores/useUserStore";
// 创建用户状态管理实例
const userStore = useUserStore();
// 创建路由实例
const router = useRouter();

// 退出登录
const handleCommand = command => {
  if (command !== "loginout") return router.push("/user/" + command);
  ElMessageBox.confirm("确认退出当前帐户吗？", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
      .then(() => {
        //用户点击了确认
        userStore.clearToken();
        ElMessage({type: "success", message: "账户已退出"});
        router.replace("/login");
      })
      .catch(() => {
        //用户点击了取消
        ElMessage({message: "操作已取消"});
      });
};
onMounted(() => {
  const token = localStorage.getItem("token");
  if (userStore.token) {
    userStore.setAccount();
  } else if (token) {
    userStore.setToken(token);
  } else {
    ElMessage({type: "error", message: "系统超时，请重新登录"});
    router.replace("/login");
  }
});
</script>

<template>
  <el-container v-if="userStore.account" class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="el-aside__logo" @click="router.push('/')"></div>
      <el-menu active-text-color="#ffd04b" background-color="#232323" router text-color="#fff">
        <el-menu-item index="/article/manage">
          <el-icon>
            <Promotion/>
          </el-icon>
          <span>文章管理</span>
        </el-menu-item>
        <el-menu-item index="/article/category">
          <el-icon>
            <Management/>
          </el-icon>
          <span>文章分类</span>
        </el-menu-item>
        <el-sub-menu>
          <template #title>
            <el-icon>
              <UserFilled/>
            </el-icon>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/user/info">
            <el-icon>
              <User/>
            </el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item index="/user/avatar">
            <el-icon>
              <Crop/>
            </el-icon>
            <span>更换头像</span>
          </el-menu-item>
          <el-menu-item index="/user/password">
            <el-icon>
              <EditPen/>
            </el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>
          当前帐户昵称：
          <strong>{{ userStore.account.nickname }}</strong>
        </div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="userStore.account.userPic"/>
            <el-icon>
              <CaretBottom/>
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item :icon="User" command="info">基本资料</el-dropdown-item>
              <el-dropdown-item :icon="Crop" command="avatar">更换头像</el-dropdown-item>
              <el-dropdown-item :icon="EditPen" command="password">重置密码</el-dropdown-item>
              <el-dropdown-item :icon="SwitchButton" command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
        <el-row>
          <el-col :span="24">
            <RouterView/>
          </el-col>
        </el-row>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>大事件 ©2023 Created by ihu</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url("@/assets/logo.png") no-repeat center / 120px auto;
      cursor: pointer;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>
